/* eslint-disable no-undef */
const LoadFeature = {
    loadFile: function (option) {
        var resultOption = $.extend({
            'fileUrl': undefined,
            'onLoad': function () { },
            'onError': function () { }
        }, option)
        var fileUrl = resultOption.fileUrl
        LoadFeature.loadFiles({
            'fileUrls': [fileUrl],
            'onAllLoad': function (fileUrlAndPathMap) {
                var filePath = fileUrlAndPathMap[fileUrl]
                resultOption.onLoad(filePath)
            },
            'onError': function () {
                resultOption.onError()
            }
        })
    },
    loadFiles: function (option) {
        var resultOption = $.extend({
            'fileUrls': [],
            'onProcess': function () { }, // 每上传一个文件，触发此方法一次
            'onAllLoad': function () { }, // 当上传完所有文件后，触发此方法
            'onError': function () { }
        }, option)

        var fileUrls = resultOption.fileUrls

        var totalCount = fileUrls.length
        var loadedCount = 0
        var fileUrlAndPathMap = {}
        $.each(fileUrls, function (index, fileUrl) {
            Inner.data.download({
                'fileUrl': fileUrl,
                'onSuccess': function (downloadedFilePath) {
                    fileUrlAndPathMap[fileUrl] = downloadedFilePath
                    loadedCount++
                    resultOption.onProcess(loadedCount)
                    if (loadedCount == totalCount) {
                        resultOption.onAllLoad(fileUrlAndPathMap)
                    }
                },
                'onError': function () {
                    resultOption.onError()
                }
            })
        })
    }
}

var Inner = {
    variable: {
        'tempFileNameKey': 'load'
    },
    cache: {
        'loadOption': undefined
    },
    data: {
        download: function (option) {
            var resultOption = $.extend({
                'fileUrl': undefined,
                'onSuccess': function () { },
                'onError': function () { }
            }, option)

            var fileUrl = resultOption.fileUrl
            fileUrl = fileUrl.replace('.nomedia/', '')
            FileFeature.getTempDirectory({
                'fileNameKey': Inner.variable.tempFileNameKey,
                'onSuccess': function (tempDirectory) {
                    var directoryPath = Inner.data.getDirectoryPath(fileUrl)
                    var fileName = Inner.data.getFileName(fileUrl)
                    FileFeature.getDirectoryByPath({
                        'parentDirectoryEntry': tempDirectory,
                        'directoryPath': directoryPath,
                        'onSuccess': function (directoryEntry) {
                            FileFeature.getFile({
                                'directoryEntry': directoryEntry,
                                'fileName': fileName,
                                'onSuccess': function (targetFileEntry) {
                                    var filePath = targetFileEntry.toInternalURL()
                                    resultOption.onSuccess(filePath)
                                },
                                'onNotFound': function () {
                                    var resizeFileUrl
                                    var isImageFile = Inner.data.isImageFile(fileName)
                                    if (isImageFile) {
                                        resizeFileUrl = UrlUtil.appendImageResizeSuffix(
                                            fileUrl)
                                    } else {
                                        resizeFileUrl = fileUrl
                                    }
                                    FileFeature.download({
                                        'fileUrl': resizeFileUrl,
                                        'directoryEntry': directoryEntry,
                                        'fileName': fileName,
                                        'onSuccess': function (downloadedFileEntry) {
                                            var downloadedFilePath =
                                                downloadedFileEntry.toInternalURL()
                                            resultOption.onSuccess(downloadedFilePath)
                                        },
                                        'onError': function () {
                                            resultOption.onError()
                                        }
                                    })
                                },
                                'onError': function (error) {
                                    console.error('feature/load Inner.data.download() error : '
                                        + JSON.stringify(error))
                                    resultOption.onError()
                                }
                            })
                        },
                        'onError': function () {
                            resultOption.onError()
                        }
                    })
                },
                'onError': function () {
                    resultOption.onError()
                }
            })
        },
        getFileName: function (url) {
            var fileName = url.replace(/\?.*/, '')
            fileName = fileName.replace(/.*\//, '')
            return fileName
        },
        getDirectoryPath: function (url) {
            var directoryPath = url.replace(/http[s]*.*\/\/[^/]*\//, '/')
            directoryPath = directoryPath.replace(/\?.*/, '')
            var fileName = Inner.data.getFileName(url)
            directoryPath = directoryPath.replace(fileName, '')
            return directoryPath
        },
        isImageFile: function (fileName) {
            if (StringUtil.contains(fileName, '.jpg')
                || StringUtil.contains(fileName, '.jpeg')
                || StringUtil.contains(fileName, '.png')) {
                return true
            } else {
                return false
            }
        }
    }
}
export default LoadFeature